package com.Ajava1704.for线程池2;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @类名: com.Ajava1704.for线程池2
 * @作者: lgt
 * @创建时间: 2020-02-11 20:10
 * @描述:
 **/
public class ForPoolTest4 {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("=====任务====");
            }
        };

        scheduledExecutorService.scheduleWithFixedDelay(runnable,3,5, TimeUnit.SECONDS);
        /*第一次调度开始时间点 = 当前时间 + initialDelay
        下一次调度开始时间点 = 上一次task完成时间点 + delay */

        scheduledExecutorService.scheduleAtFixedRate(runnable,3,5,TimeUnit.SECONDS);
        /*解释
        第一次调度开始时间点=当前时间+initialDelay
        第二次调度开始时间点=initialDelay + perid
        第n次调度开始时间点=initialDelay + n *perid */

        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("=====任务二======");
            }
        };
        scheduledExecutorService.scheduleWithFixedDelay(runnable1,3,1,TimeUnit.SECONDS);
    }
}
